package cn.edu.swu.auth;

import cn.edu.swu.core.AbstractRepo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.function.Function;

public class UserRepo extends AbstractRepo {

    public void testConnection() throws SQLException {
        try (Connection conn = getConnection()) {
            if (conn == null) {
                throw new SQLException("无法建立数据库连接");
            }
        }
    }



    public <T> T query(String sql, Function<PreparedStatement, T> queryHandler) throws SQLException {
        try (PreparedStatement ps = this.getConnection().prepareStatement(sql)) {
            return queryHandler.apply(ps);
        }
    }


    public <T> T execute(String sql, Function<PreparedStatement, T> executeHandler) throws SQLException {
        try (PreparedStatement ps = this.getConnection().prepareStatement(sql)) {
            return executeHandler.apply(ps);
        }
    }
}
